package test;

import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.select.*;
import org.junit.Test;

import java.io.StringReader;
import java.util.List;

/**
 * @author Tang
 * @classname NoContext
 * @description [  ]
 * @date 2021/5/23 9:53
 */
public class NoContext {

    private final String sql = "select age,name from tb_user order by name limit 1,2";

    @Test
    public void test02() {

    }

    @Test
    public void test01() throws Exception {
        CCJSqlParserManager parserManager = new CCJSqlParserManager();
        Select select = (Select) parserManager.parse(new StringReader(sql));
        PlainSelect plainSelect = (PlainSelect) select.getSelectBody();

        Limit limit1 = new Limit();
//        limit1.setRowCount(new );
        plainSelect.setLimit(limit1);

        List<SelectItem> selectItems = plainSelect.getSelectItems();
        selectItems.forEach(item -> {
            System.out.println("selectItems ---> " + item.toString() + "--->" + item.getASTNode());
        });
        Limit limit = plainSelect.getLimit();
        System.out.println(limit.getOffset() + "--->" + limit.getRowCount());
        List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
        orderByElements.forEach(item -> {
            System.out.println("order by ---> " + item.toString() + "--->" + item.isAsc());
        });
    }

}